package com.sophossystems.client.asmon.dao.spring;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.transaction.annotation.Transactional;

import com.sophossystems.client.asmon.dao.ClienteDao;
import com.sophossystems.client.asmon.dto.ClienteDTO;
import com.sophossystems.client.asmon.dto.ClientePkDTO;
import com.sophossystems.client.asmon.exceptions.DaoException;
import com.sophossystems.client.asmon.hibernate.Cliente;

public class ClienteDaoImpl extends AbstractDAO implements ClienteDao {

	
	
	private static Logger logger = Logger.getLogger(ClienteDaoImpl.class);
	/**
	 * Method 'insert'
	 * 
	 * @param dto
	 * @return ClientePk
	 */
	@Override
	@Transactional
	public ClientePkDTO insert(final ClienteDTO dto) {
		ClientePkDTO exito = null;
		try {
			exito = (ClientePkDTO) getHibernateTemplate().execute(
					new HibernateCallback() {
						/**
						 * {@inheritDoc}
						 * 
						 * @see org.springframework.orm.hibernate3.HibernateCallback#doInHibernate(org.hibernate.Session)
						 */
						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							Transaction tx = session.beginTransaction();
							ClientePkDTO pk = null;
							Cliente cliente = new Cliente();
							cliente.mapper(dto);
							session.save(cliente);
							
							
							tx.commit();
							int afterInsert = 0;
							try {
								afterInsert = findLastInsert().getIdCliente();
								pk = new ClientePkDTO();
								pk.setIdCliente(afterInsert);

							} catch (DaoException es) {
								throw new HibernateException("Insersion: "
										+ es.getMessage());
							}
							return pk;
						}
					});
		} catch (final Exception e) {
			logger.error("Error: insert ClienteDaoImpl, cause: " + e.getMessage(), e);

		}
		return exito;

	}

	/**
	 * Updates a single row in the CLIENTE table.
	 */
	@Override
	@Transactional
	public void update(final ClienteDTO dto) throws DaoException {
		try {
			getHibernateTemplate().execute(new HibernateCallback() {

				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {

					ClientePkDTO pk = null;
					ClienteDTO res = null;
					Transaction tx = session.beginTransaction();
					// Aqui primero se busca si existe el registro
					try {
						res = findByPrimaryKey(dto.getIdCliente());
						pk = new ClientePkDTO();
						pk.setIdCliente(res.getIdCliente());
					} catch (DaoException es) {
						throw new HibernateException("Update ClientDao: "
								+ es.getMessage());
					}
					// /aqui se actualizar los campos y con los de la entrada
					// cuidado de actualiza todo.
					Cliente persis = new Cliente();
					persis.mapper(dto);
					persis.setIdCliente(res.getIdCliente());
					session.update(persis);
					tx.commit();
					return null;
				}
			});
		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getCause(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		// jdbcTemplate.update("UPDATE " + getTableName() +
		// " SET idCliente = ?, idTipoCliente = ?, id_direccion = ?, idStatus = ?, idRecolectaDoc = ?, idContadorAsig = ?, idCoordinador = ?, idContactoVenta = ?, fechaCaptura = ? WHERE idCliente = ?",dto.getIdCliente(),dto.getIdTipoCliente(),dto.getIdDireccion(),dto.getIdStatus(),dto.getIdRecolectaDoc(),dto.getIdContadorAsig(),dto.getIdCoordinador(),dto.getIdContactoVenta(),dto.getFechaCaptura(),pk.getIdCliente());
	}

	/**
	 * Deletes a single row in the CLIENTE table.
	 */
	@Override
	@Transactional
	public void delete(final ClientePkDTO pk) throws DaoException {
		try {
			getHibernateTemplate().execute(new HibernateCallback() {

				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Transaction tx = session.beginTransaction();
					Cliente cliente = (Cliente) session.load(Cliente.class,
							pk.getIdCliente());
					session.delete(cliente);
					tx.commit();
					return cliente;
				}
			});
		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getCause(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}

		// jdbcTemplate.update("DELETE FROM " + getTableName() +
		// " WHERE idCliente = ?",pk.getIdCliente());
	}

	/**
	 * Method 'mapRow'
	 * 
	 * @param rs
	 * @param row
	 * @throws SQLException
	 * @return Cliente
	 */
	public Cliente mapRow(ResultSet rs, int row) throws SQLException {
		Cliente dto = new Cliente();
		dto.setIdCliente(rs.getInt(1));
		dto.setIdTipoCliente(rs.getInt(2));
		if (rs.wasNull()) {
			dto.setIdTipoClienteNull(true);
		}

		dto.setIdDireccion(rs.getInt(3));
		if (rs.wasNull()) {
			dto.setIdDireccionNull(true);
		}

		dto.setIdStatus(rs.getInt(4));
		if (rs.wasNull()) {
			dto.setIdStatusNull(true);
		}

		dto.setIdRecolectaDoc(rs.getInt(5));
		if (rs.wasNull()) {
			dto.setIdRecolectaDocNull(true);
		}

		dto.setIdContadorAsig(rs.getInt(6));
		if (rs.wasNull()) {
			dto.setIdContadorAsigNull(true);
		}

		dto.setIdCoordinador(rs.getInt(7));
		if (rs.wasNull()) {
			dto.setIdCoordinadorNull(true);
		}

		dto.setIdContactoVenta(rs.getInt(8));
		if (rs.wasNull()) {
			dto.setIdContactoVentaNull(true);
		}

		dto.setFechaCaptura(rs.getTimestamp(9));
		return dto;
	}

	/**
	 * Method 'Name'
	 * 
	 * @return String
	 */
	public String getTableName() {
		return "Cliente";
	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'idCliente = :idCliente'.
	 */
	@Override
	@Transactional
	public ClienteDTO findByPrimaryKey(final int idCliente) throws DaoException {
		ClienteDTO cliente = null;
		try {
			cliente = (ClienteDTO) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {

							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente, c.rfc as rfc,c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.idCliente = :idCliente");

							oQuery.setInteger("idCliente", idCliente);

							ClienteDTO clienteDto = (ClienteDTO) oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list().get(0);
							return clienteDto;
						}
					});

			// jdbcTemplate.query("SELECT idCliente, idTipoCliente, id_direccion, idStatus, idRecolectaDoc, idContadorAsig, idCoordinador, idContactoVenta, fechaCaptura FROM "
			// + getTableName() + " WHERE idCliente = ?", this,idCliente);
			// return list.size() == 0 ? null : list.get(0);
		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return (ClienteDTO) cliente;

	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria ''.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findAll() throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {

							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente, c.rfc as rfc,c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c ORDER BY c.idCliente");
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});

		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;

	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'idCliente = :idCliente'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereIdClienteEquals(final int idCliente)
			throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {

							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " WHERE c.idCliente = :idCliente ORDER BY c.idCliente");
							oQuery.setInteger("idCliente", idCliente);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});

		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;

	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'idTipoCliente = :idTipoCliente'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereIdTipoClienteEquals(final int idTipoCliente)
			throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {

							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.idTipoCliente = :idTipoCliente"
											+ " ORDER BY c.idTipoCliente");
							oQuery.setInteger("idTipoCliente", idTipoCliente);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});

		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;

	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'id_direccion = :idDireccion'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereIdDireccionEquals(final int idDireccion)
			throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {

							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.id_direccion = :id_direccion ORDER BY c.id_direccion");
							oQuery.setInteger("id_direccion", idDireccion);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});

		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;
	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria 'idStatus
	 * = :idStatus'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereIdStatusEquals(final int idStatus)
			throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {

							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente, c.rfc as rfc,c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE idStatus = :idStatus ORDER BY c.idStatus");
							oQuery.setInteger("idStatus", idStatus);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});
		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;
	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'idRecolectaDoc = :idRecolectaDoc'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereIdRecolectaDocEquals(
			final int idRecolectaDoc) throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {

							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.idRecolectaDoc = :idRecolectaDoc ORDER BY c.idRecolectaDoc");
							oQuery.setInteger("idRecolectaDoc", idRecolectaDoc);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});
			
		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;
	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'idContadorAsig = :idContadorAsig'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereIdContadorAsigEquals(
			final int idContadorAsig) throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
					
							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.idContadorAsig = :idContadorAsig ORDER BY c.idContadorAsig");
							oQuery.setInteger("idContadorAsig", idContadorAsig);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});

		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;
	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'idCoordinador = :idCoordinador'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereIdCoordinadorEquals(final int idCoordinador)
			throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.idCoordinador = :idCoordinador ");
							oQuery.setInteger("idCoordinador", idCoordinador);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});
		
		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;

	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'idContactoVenta = :idContactoVenta'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereIdContactoVentaEquals(
			final int idContactoVenta) throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							
							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.idContactoVenta = :idContactoVenta ");
							oQuery.setInteger("idContactoVenta", idContactoVenta);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});
		
		} catch (final Exception e) {
			logger.error("Error: getCliente, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getCliente, cause: "
					+ e.getMessage());
		}
		return cliente;

	}

	/**
	 * Returns all rows from the CLIENTE table that match the criteria
	 * 'fechaCaptura = :fechaCaptura'.
	 */
	@Override
	@SuppressWarnings("unchecked")
	@Transactional
	public List<ClienteDTO> findWhereFechaCapturaEquals(final Date fechaCaptura)
			throws DaoException {
		List<ClienteDTO> cliente = null;
		try {
			cliente = (List<ClienteDTO>) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
						
							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.fechaCaptura = :fechaCaptura ");
							oQuery.setDate("fechaCaptura", fechaCaptura);
							List<ClienteDTO> clienteDto = oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
									.list();
							return clienteDto;
						}
					});
			
		} catch (final Exception e) {
			logger.error("Error: getActivity, cause: " + e.getMessage(), e);
			throw new DaoException("Error: getActivity, cause: "
					+ e.getMessage());
		}

		return cliente;

	}

	/**
	 * Returns the rows from the CLIENTE table that matches the specified
	 * primary-key value.
	 */
	@Override
	public ClienteDTO findByPrimaryKey(ClientePkDTO pk) throws DaoException {
		return findByPrimaryKey(pk.getIdCliente());
	}
	
	
	@Override
	@Transactional
	public ClienteDTO findByRfc(final String rfc) throws DaoException
	{
		ClienteDTO cliente = null;
		try {
			cliente = (ClienteDTO) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
						
							final Query oQuery = session
									.createQuery("SELECT c.idCliente as idCliente,c.rfc as rfc, c.idTipoCliente as idTipoCliente, c.idDireccion as idDireccion,"
											+ " c.idStatus as idStatus, c.idRecolectaDoc as idRecolectaDoc, c.idContadorAsig as idContadorAsig,"
											+ " c.idCoordinador as idCoordinador, c.idContactoVenta as idContactoVenta, c.fechaCaptura as fechaCaptura FROM "
											+ getTableName()
											+ " c WHERE c.rfc = :rfc ");
							oQuery.setString("rfc", rfc);
							ClienteDTO clienteDto = (ClienteDTO) oQuery
									.setResultTransformer(
											Transformers
													.aliasToBean(ClienteDTO.class))
													.list().get(0);
							return clienteDto;
						}
					});
			
		} catch (final Exception e) {
			logger.error("Error: FindByRfc, cause: " + e.getMessage(), e);
			throw new DaoException("Error: FindByRfc, cause: "
					+ e.getMessage());
		}

		return cliente;
		
	}

	@Override
	public ClientePkDTO findLastInsert() throws DaoException {
		ClientePkDTO cliente = null;
		try {
			cliente = (ClientePkDTO) getHibernateTemplate().execute(
					new HibernateCallback() {

						@Override
						public Object doInHibernate(Session session)
								throws HibernateException, SQLException {
							
							Query oQuery = session
									.createQuery("SELECT max(c.idCliente) as idCliente FROM "
											+ getTableName()
											+ " c ");
							
							ClientePkDTO clienteDto = (ClientePkDTO) oQuery
									.setResultTransformer(Transformers
											.aliasToBean(ClientePkDTO.class)).list().get(0);
							return clienteDto;
						}
					});
		
		} catch (Exception e) {
			logger.error("Error: ClienteDaoImpl, cause: " + e.getMessage(), e);
			throw new DaoException("Query failed", e);
		}
		return cliente;

	}
	
	
	
	
}
